package com.d3.pomodoro.component

import android.content.Context
import android.util.AttributeSet
import android.view.View
import android.animation.ValueAnimator
import android.graphics.*
import com.orhanobut.logger.Logger


class ArcProgress(context: Context, attrs: AttributeSet) : View(context, attrs) {
    private var paint: Paint? = null
    private var radious: Float = 0f
    private var mPath: Path? = null
    private var pos: FloatArray? = null
    private var tan: FloatArray? = null
    private var mMeasure: PathMeasure? = null

    private var valueAnimator: ValueAnimator? = null
    private var mAnimatorValue: Float = 0.toFloat()
    private var percent = 0f

    private var sweepAngle: Float = 0f


    init {
        paint = Paint()
        paint?.isAntiAlias = true

        mPath = Path()
        mMeasure = PathMeasure()
        pos = FloatArray(2)
        tan = FloatArray(2)

        valueAnimator = ValueAnimator.ofFloat(0f, 1f)
//        valueAnimator?.apply {
//            addUpdateListener { valueAnimator ->
//                mAnimatorValue = valueAnimator.animatedValue as Float
//                val degree = 100 / 100 * 180
//                Logger.e((mAnimatorValue * degree).toString() + "")
//                sweepAngle = (mAnimatorValue * degree)
//                invalidate()
//            }
//            duration = 3000
//        }
    }

    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        val widthMode = MeasureSpec.getMode(widthMeasureSpec)
        val heightMode = MeasureSpec.getMode(heightMeasureSpec)
        val width = MeasureSpec.getSize(widthMeasureSpec)
        val height = MeasureSpec.getSize(heightMeasureSpec)

        val defaultWidthSize = 1200
        val defaultHeightSize = 600

        if (widthMode == MeasureSpec.AT_MOST && heightMode == MeasureSpec.AT_MOST) {
            setMeasuredDimension(defaultWidthSize, defaultHeightSize)
        } else if (widthMode == MeasureSpec.AT_MOST) {
            setMeasuredDimension(defaultWidthSize, height)
        } else if (heightMode == MeasureSpec.AT_MOST) {
            setMeasuredDimension(width, defaultHeightSize)
        } else {
            setMeasuredDimension(width, height)
        }
    }

    override fun onDraw(canvas: Canvas?) {
        super.onDraw(canvas)
        val rectF = RectF()
        if (paint != null) {
            radious = Math.min(width, height) / 2 - paint!!.strokeWidth - 100
            rectF.left = (width / 2 - radious)
            rectF.top = (height / 2 - radious)
            rectF.right = (width / 2 + radious)
            rectF.bottom = (height / 2 + radious)

            paint!!.apply {
                color = Color.parseColor("#44000000")
                style = Paint.Style.STROKE
                strokeWidth = 10f


            }
            canvas?.drawArc(rectF, 0f, 180f, false, paint!!)

            mPath?.addArc(rectF, 0f, 180f)
            mMeasure?.setPath(mPath, false)
            mMeasure?.getPosTan(mMeasure!!.length, pos, tan)
            //canvas.drawCircle(pos[0], pos[1], 10, paint);

            paint?.color = Color.GREEN
            canvas?.drawArc(rectF, 0f, sweepAngle, false, paint!!)
        }

    }

    fun startToProgress(percent: Long) {
        valueAnimator?.apply {
            addUpdateListener { valueAnimator ->
                mAnimatorValue = valueAnimator.animatedValue as Float
                val degree = 100 / 100 * 180
                Logger.e((mAnimatorValue * degree).toString() + "")
                sweepAngle = (mAnimatorValue * degree)
                invalidate()
            }
            duration = percent
            start()
        }
    }


}